package com.zxl.always.on.display.ui.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zxl.always.on.display.ApplicationHelper;
import com.zxl.always.on.display.R;
import com.zxl.always.on.display.model.ClockStyle;
import com.zxl.always.on.display.model.bean.ClockInfo;

/**
 * Created by Administrator on 2018/3/2.
 */

public class SettingPreviewItemView extends LinearLayout {

    private boolean mIsChecked;

    private TextView mTitleView;

    private ImageView mImageView, mSelectImageView;

    private ClockStyle mClockStyle;

    private int itemSize;

    public SettingPreviewItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mTitleView = findViewById(R.id.wallpaper_none_text);
        mImageView = findViewById(R.id.wallpaper_preview_image);
        mSelectImageView = findViewById(R.id.wallpaper_selected_image);
    }

    public void setChecked(boolean mIsChecked) {
        this.mIsChecked = mIsChecked;
        mSelectImageView.setVisibility(mIsChecked ? View.VISIBLE : View.GONE);
        mTitleView.setTextColor(getResources().getColor(mIsChecked ? R.color.settings_wallpaper_selected_color : R.color.settings_guide_description_summary));
    }

    public void setClockStyle(ClockStyle mClockStyle, int itemSize) {
        this.mClockStyle = mClockStyle;
        this.itemSize = itemSize;
        refreshClockStyle();
    }

    public void refreshClockStyle() {
        mTitleView.setText(mClockStyle.getTitle());
        new Thread(){

            @Override
            public void run() {
                super.run();
                final Bitmap bitmap = mClockStyle.getSettingPreview(getContext(), new ClockInfo(mClockStyle), itemSize);
                ApplicationHelper.postRunInUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mImageView.setImageBitmap(bitmap);
                    }
                });
            }
        }.start();
    }


    public ClockStyle getClockStyle() {
        return mClockStyle;
    }

    public boolean isChecked() {
        return mIsChecked;
    }
}
